package com.yanggu.jt808.handler;

import com.yanggu.jt808.entity.annotation.Jt808Pack;
import com.yanggu.jt808.entity.gpsdata.DataPackage;
import com.yanggu.jt808.util.Jt808Constant;
import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;

/**
 * 终端注册
 * 终端注册是为了建立终端和车辆的关系 注册获取鉴权码鉴权完成后才能传输
 */
@Slf4j
@Jt808Pack(msgId = 0x0100)
public class Handler0x0100 implements PackHandler {

    @Override
    public DataPackage handle(DataPackage msg, int flowId) {
        DataPackage.Header header = msg.getHeader();
        ByteBuf msgBody = msg.getMsgBody();

        //获取终端手机号
        String terminalPhone = header.getTerminalPhone();
        log.info("[终端注册 0x0100] 终端手机号: {}", terminalPhone);

        boolean version2019 = header.isVersion2019();

        //解析终端注册的数据包
        byte[] producerId, terminalType, terminalId, licenseByteArray;
        //省域ID
        int provinceId = msgBody.readUnsignedShort();
        //市县域ID
        int cityId = msgBody.readUnsignedShort();
        if (version2019) {
            producerId = new byte[11];
            terminalType = new byte[30];
            terminalId = new byte[30];
        } else {
            producerId = new byte[5];
            terminalType = new byte[20];
            terminalId = new byte[7];
        }
        //制造商ID
        msgBody.readBytes(producerId);
        //终端型号
        msgBody.readBytes(terminalType);
        //终端ID
        msgBody.readBytes(terminalId);
        //车牌颜色
        short licenseColor = msgBody.readUnsignedByte();
        licenseByteArray = new byte[msgBody.readableBytes()];
        msgBody.readBytes(licenseByteArray);
        //车牌
        String license = new String(licenseByteArray, Jt808Constant.DEFAULT_CHARSET);



        return null;
    }
}
